package argo.review;

import java.util.HashMap;
import java.util.Map;

/**
 * 两束之和
 */
public class TwoSum {

    public int[] twoSum(int[] nums, int target) {

        int[] res = new int[2];

        if (nums == null || nums.length < 2) {
            return res;
        }

        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int sub = target - nums[i];
            if (map.containsKey(sub)) {
                res[0] = i;
                res[1] = map.get(sub);
            }
            map.put(nums[i], i);
        }

        return res;
    }
}
